package com.jingtai.shop.controller.user;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jingtai.common.template.ResultMapInfo;
import com.jingtai.core.template.BaseController;
import com.jingtai.shop.service.user.MemberInfoService;
import com.jingtai.shop.webfacade.bo.user.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;


/**
 * @author GR
 */
@Api(tags = "商城-用户信息" )
@RestController
@RequestMapping("/shop/sso")
@CrossOrigin(origins = "*")
public class UserInfoController extends BaseController {

    @Resource
    private MemberInfoService memberInfoService;

    /**
     * 用户注册
     */
    @ApiOperation(value = "用户注册")
    @ApiOperationSupport(ignoreParameters = {"bo.platform","bo.version"})
    @PostMapping("/userRegister")
    public Map<String, Object> userRegister(@Valid @RequestBody UserRegisterBO bo) {
        bo.validate();
        boolean userRegisterStatus = memberInfoService.userRegister(bo);
        return returnResultMap(userRegisterStatus ? ResultMapInfo.SUBMITSUCCESS : ResultMapInfo.SUBMITFAIL);
    }

    /**
     * 用户登录
     */
    @ApiOperation(value = "用户登录")
    @ApiOperationSupport(ignoreParameters = {"bo.platform","bo.version"})
    @PostMapping("/userLogin")
    public Map<String, Object> userLogin(@RequestBody UserLoginBO bo) {
        bo.validate();
        Map<String, String> token = memberInfoService.userLogin(bo);
        return returnResultMap(ResultMapInfo.LOGINSUCCESS, token);
    }

    /**
     * 用户登出
     */
    @ApiOperation(value = "用户登出")
    @PostMapping("/userLogout")
    public Map<String, Object> userLogout(HttpServletRequest request) {
        memberInfoService.userLogout(request);
        return returnResultMap(ResultMapInfo.LOGOTSUCCESS);
    }

    /**
     * 忘记密码
     */
    @ApiOperation(value = "忘记密码")
    @ApiOperationSupport(ignoreParameters = {"bo.platform","bo.version"})
    @PostMapping("/userForgetPwd")
    public Map<String, Object> userForgetPwd(@RequestBody UserForgetPwdBO bo) {
        bo.validate();
        boolean updateStatus = memberInfoService.userForgetPwd(bo);
        return updateStatus ? returnResultMap(ResultMapInfo.OPERATE_SUCCESS) : returnResultMap(ResultMapInfo.OPERATE_FAIL);
    }

    /**
     * 用户获取验证码
     */
    @ApiOperation(value = "用户获取验证码")
    @ApiOperationSupport(ignoreParameters = {"bo.platform","bo.version"})
    @PostMapping("/userObtainCode")
    public Map<String, Object> userObtainCode(@RequestBody UserObtainCodeBO bo) {
        bo.validate();
        boolean sendSatus = memberInfoService.userObtainCode(bo);
        return sendSatus ? returnResultMap(ResultMapInfo.SENDSUCCESS) : returnResultMap(ResultMapInfo.SENDFAIL);
    }

    /**
     * 用户-修改个人信息
     */
    @ApiOperation(value = "用户-修改个人信息")
    @ApiOperationSupport(ignoreParameters = {"bo.platform","bo.version"})
    @PostMapping("/editUserInfo")
    public Map<String, Object> editUserInfo(@RequestBody EditUserInfoBO bo) {
        bo.validate();
        boolean editStatus = memberInfoService.editUserInfo(bo);
        return editStatus ? returnResultMap(ResultMapInfo.OPERATE_SUCCESS) : returnResultMap(ResultMapInfo.OPERATE_FAIL);
    }

    /**
     * 用户-注销用户
     */
    @ApiOperation(value = "用户-注销用户")
    @PostMapping("/userDestroy")
    public Map<String, Object> userDestroy(HttpServletRequest request) {
        boolean destroyStatus = memberInfoService.userDestroy(request);
        return destroyStatus ? returnResultMap(ResultMapInfo.OPERATE_SUCCESS) : returnResultMap(ResultMapInfo.OPERATE_FAIL);
    }
}
